package map;

import tree.TreeNode;

/**
 * Created by fengliejv on 2017/9/7.
 */
public class ConstructStringfromBinaryTree606 {
    public String tree2str(TreeNode t) {
        StringBuilder result = new StringBuilder();
        if (t == null) {
            return "";
        }
        result.append(t.val);
        if(t.left!=null||t.right!=null){
            String left = tree2str(t.left);
            result.append("(");
            result.append(left);
            result.append(")");
            String right = tree2str(t.right);
            if (!"".equals(right)) {
                result.append("(");
                result.append(right);
                result.append(")");
            }
        }

        return result.toString();
    }


    class Solution {
        public String tree2str(TreeNode t) {
            StringBuilder sb = new StringBuilder();
            search(t, sb);
            return sb.toString();
        }

        public void search(TreeNode t, StringBuilder sb) {
            if (t == null) {
                return;
            }
            sb.append(t.val);
            if (t.left != null) {
                sb.append("(");
                search(t.left, sb);
                sb.append(")");
            } else {
                if (t.right != null) {
                    sb.append("()");
                }
            }
            if (t.right != null) {
                sb.append("(");
                search(t.right, sb);
                sb.append(")");
            }

        }

    }
}
